home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / threadheap.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-06-18  |  5.5 KB  |  194 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. //////////////////////////////////////////////////////////////////////////////
  22. //
  23. // Note: This file was modified by Crystal Decisions in June 2002.
  24. //
  25. //////////////////////////////////////////////////////////////////////////////
  26.  
  27.  
  28. #ifndef _THREADHEAP_H_
  29. #define _THREADHEAP_H_
  30.  
  31. #include "config.h"
  32.  
  33. #include <string.h>
  34.  
  35. #include "heap.h"
  36.  
  37. class processHeap; // forward declaration
  38.  
  39. //
  40. // We use one threadHeap for each thread (processor).
  41. //
  42.  
  43. class threadHeap : public hoardHeap {
  44.  
  45. public:
  46.  
  47.   threadHeap (void);
  48.  
  49.   // Memory allocation routines.
  50.   void * malloc (const size_t sz);
  51.   inline void * memalign (size_t alignment, size_t sz);
  52.  
  53.   // Find out how large an allocated object is.
  54.   inline static size_t objectSize (void * ptr);
  55.  
  56. // The Win32 version of Crystal Hoard needs to know the actual size of
  57. // the block, to support Microsoft's _msize() extension to the CRT.
  58. #if defined(CRYSTAL_HOARD) && defined(WIN32)
  59.   inline static size_t getActualSize (void* ptr);
  60. #endif
  61.  
  62.   // Set our process heap.
  63.   inline void setpHeap (processHeap * p);
  64.  
  65. private:
  66.  
  67.   // Prevent copying and assignment.
  68.   threadHeap (const threadHeap&);
  69.   const threadHeap& operator= (const threadHeap&);
  70.  
  71.   // Our process heap.
  72.   processHeap *    _pHeap;
  73.  
  74.   // We insert a cache pad here to avoid false sharing (the
  75.   // processHeap holds an array of threadHeaps, and we don't want
  76.   // these to share any cache lines).
  77.   double _pad[CACHE_LINE / sizeof(double)];
  78. };
  79.  
  80.  
  81. void * threadHeap::memalign (size_t alignment,
  82.                  size_t size)
  83. {
  84.   // Calculate the amount of space we need
  85.   // to satisfy the alignment requirements.
  86.  
  87.   size_t newSize;
  88.  
  89.   // If the alignment is less than the required alignment,
  90.   // just call malloc.
  91.   if (alignment <= ALIGNMENT) {
  92.     return this->malloc (size);
  93.   }
  94.  
  95.   if (alignment < sizeof(block)) {
  96.     alignment = sizeof(block);
  97.   }
  98.  
  99.   // Alignment must be a power of two!
  100.   assert ((alignment & (alignment - 1)) == 0);
  101.  
  102.   // Leave enough room to align the block within the malloced space.
  103.   newSize = size + sizeof(block) + alignment;
  104.  
  105.   // Now malloc the space up with a little extra (we'll put the block
  106.   // pointer in right behind the allocated space).
  107.  
  108.   void * ptr = this->malloc (newSize);
  109.   if ((((unsigned long) ptr) & -((long) alignment)) == 0) {
  110.     // ptr is already aligned, so return it.
  111.     assert (((unsigned long) ptr % alignment) == 0);
  112.     return ptr;
  113.  
  114.   } else {
  115.  
  116.     // Align ptr.
  117.     char * newptr = (char *)
  118.       (((unsigned long) ptr + alignment - 1) & -((long) alignment));
  119.  
  120.     // If there's not enough room for the block header, skip to the
  121.     // next aligned space within the block..
  122.     if ((unsigned long) newptr - (unsigned long) ptr < sizeof(block)) {
  123.       newptr += alignment;
  124.     }
  125.     assert (((unsigned long) newptr % alignment) == 0);
  126.  
  127.     // Copy the block from the start of the allocated memory.
  128.     block * b = ((block *) ptr - 1);
  129.  
  130.     assert (b->isValid());
  131.     assert (b->getSuperblock()->isValid());
  132.     
  133.     // Make sure there's enough room for the block header.
  134.     assert (((unsigned long) newptr - (unsigned long) ptr) >= sizeof(block));
  135.  
  136.     block * p = ((block *) newptr - 1);
  137.  
  138.     // Make sure there's enough room allocated for size bytes.
  139.     assert (((unsigned long) p - sizeof(block)) >= (unsigned long) b);
  140.  
  141.     if (p != b) {
  142.       assert ((unsigned long) newptr > (unsigned long) ptr);
  143.       // Copy the block header.
  144.       *p = *b;
  145.       assert (p->isValid());
  146.       assert (p->getSuperblock()->isValid());
  147.       
  148.       // Set the next pointer to point to b with the 1 bit set.
  149.       // When this block is freed, it will be treated specially.
  150.       p->setNext ((block *) ((unsigned long) b | 1));
  151.  
  152.     } else {
  153.       assert (ptr != newptr);
  154.     }
  155.  
  156.     assert (((unsigned long) ptr + newSize) >= ((unsigned long) newptr + size));
  157.     return newptr;
  158.   }
  159. }
  160.  
  161.  
  162. size_t threadHeap::objectSize (void * ptr) 
  163. {
  164.   // Find the superblock pointer.
  165.   
  166.   block * b = ((block *) ptr - 1);
  167.   assert (b->isValid());
  168.   superblock * sb = b->getSuperblock ();
  169.   assert (sb);
  170.   
  171.   // Return the size.
  172.   return sizeFromClass (sb->getBlockSizeClass());
  173. }
  174.  
  175. // The Win32 version of Crystal Hoard needs to know the actual size of
  176. // the block, to support Microsoft's _msize() extension to the CRT.
  177. #if defined(CRYSTAL_HOARD) && defined(WIN32)
  178. size_t threadHeap::getActualSize (void* ptr)
  179. {
  180.   block * b = ((block *) ptr - 1);
  181.   assert (b->isValid());
  182.   return b->getActualSize();
  183. }
  184. #endif
  185.  
  186.  
  187. void threadHeap::setpHeap (processHeap * p) 
  188. {
  189.   _pHeap = p; 
  190. }
  191.  
  192. #endif // _THREADHEAP_H_
  193.  
  194.